<?
/**
 * VoiceOne Settings/Provider
 *
 * Frontend to provide access to the provider system in voiceone.
 * @package voiceone
 * @subpackage  settings_provider
 */
  include ("../../../admin/config/cfg.php");
  global $cwlang;

  //================ PAGE LOGIC ================
  $alert=getAlert();
  //delete provider
  if ($_GET["cmd"]=="delProvider"){
    $ret=$asterisk->delProvider($_GET["id"]);
    if ($ret==1) $alert="<div class=\"AlertGood\">".T_("Provider has been successfully deleted")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Provider has NOT been deleted")."</div>";
    goToMainPage($alert,"./");
  //delete account
  }else if ($_GET["cmd"]=="delAccount"){
    $ret=$asterisk->delProviderAccount($_GET["id"]);
    if ($ret==1) $alert="<div class=\"AlertGood\">".T_("Provider has been successfully deleted")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Provider has NOT been deleted")."</div>";
    goToMainPage($alert,"./");
  }
  $ret=$asterisk->getProvider(0);
  if ($err=$asterisk->getError()) die("Error: $err");
  $count=0;
  if (is_array($ret)){
    $html="<tr>";
    //$html.="<tr><td colspan=\"2\" class=\"todolisttitle\"> Voip Provider </td></tr><tr>\n";
    foreach($ret as $v) {
      $id=$v["id"];
      $name=$v["name"];
      $tech=$v["technology"];
      $techArr=$asterisk->getTechnology($v["technology"]);
      if(is_array($techArr[0])) $techtag=$techArr[0]["name"]; else $techtag="UNKNOWN";
      $accounts=$v["accounts"];
      $icon=getIcon($tech);
      $html.="<td>$icon <a href=\"edit/?id=$id\">$name</a>  <a href=\"$PHP_SELF?cmd=delProvider&id=$id\" onclick=\"if(confirm('".sprintf(T_("Are you sure to remove the provider: %s ?"),$name )."')){if(document.getElementById('acclist_".$id."')==null){return true}else{if(confirm('".sprintf(T_("Provider %s include some account. Are you sure to remove all related account?"),$name )."')) return true; else return false;}}else{return false;}\"><img src=\"$relBasePath/public/img/trash.gif\" /></a> ";
      $html.="<a href=\"account/?prov=$id\" class=\"admin\" >(Add a new Account)</a>";
      if(is_array($accounts)){
        $html.="<ul id=\"acclist_".$id."\" style=\"list-style:none; margin-left: 50px;padding-top: 0.5em;\">";
        foreach($accounts as $account){
          $accountId=$account["id"];
          $accountLabel=$account["name"];
          $accountUsername=$account["username"];
          $html.="<li style=\"margin:0; padding:0\"><a  href=\"account/?id=$accountId\">$accountUsername</a>  ($techtag/$accountLabel) <a href=\"$PHP_SELF?cmd=delAccount&id=$accountId\" onclick=\"if (confirm('".sprintf(T_("Are you sure to remove the account %s ?"),$accountLabel )."')) return true; else return false;\"><img src=\"$relBasePath/public/img/trash.gif\" /></a>";
        }
        $html.="</ul>";
      }
      $html.="</td>\n";
      if ((++$count)%2==0) $html.="</tr><tr>\n";
    }
    $html.="</tr>";
  }
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=T_("Provider")?></h1>
    <h3>
      <a href="edit/?type=voip" class="admin" ><?=T_("New Voip provider")?></a> &nbsp;&nbsp;&nbsp;
      <!--<a href="edit/?type=telco" class="admin" ><?=$cwlang["provider"]["NewTelco"]?></a>-->
    </h3>
  </div>
  <table class="todolist"><?=$html?></table>

  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start();
  ?>
    <div class="Sidebar">
      <?$sel="providers";include("../sidebar.php");?>
      <h1><?=T_("Information")?></h1>
      <?=T_("<p>Resources are provider used to make calls.</p><p>There are two types of providers:<ol><li>VoIP providers</li><li>TelCo providers</li></ol></p>")?>
    </div>
  <?
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================
  $smarty->assign("section", "providers");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('settings.tpl');
?>

